package com.nike.amoeba.ui.frg

import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.AppCompatSpinner
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.google.gson.Gson
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.decoration.GridSpacingItemDecoration
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.utils.DensityUtil
import com.lxj.xpopup.XPopup
import com.nike.amoeba.R
import com.nike.amoeba.adapter.CustomSpinnerAdapter
import com.nike.amoeba.adapter.GridImageAdapter
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.bean.AppRoomDataBase
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.bean.db.AssetBean
import com.nike.amoeba.databinding.FAssetUploadBinding
import com.nike.amoeba.ext.INVENTORY_NOT
import com.nike.amoeba.ext.INVENTORY_STOCK
import com.nike.amoeba.ext.SCAN_STATUS_MANUALLY
import com.nike.amoeba.ext.UPLOAD_IMAGE_SPLIT
import com.nike.amoeba.ext.showMessage
import com.nike.amoeba.mar.eventViewModel
import com.nike.amoeba.viewmodel.AssetModel
import com.nike.amoeba.viewmodel.AssetUploadModel
import com.nike.amoeba.weight.FullyGridLayoutManager
import com.nike.amoeba.weight.GlideEngine
import com.nike.amoeba.weight.ImageFileCompressEngine
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @Author nike
 * @Date 2023/8/2 14:18
 * @Description
 */
class AssetUploadFrg: BaseFragment<AssetUploadModel, FAssetUploadBinding>(){

    private val assetModel: AssetModel by activityViewModels()

    val imageAdapter by lazy { activity?.let { GridImageAdapter(it, localMediaList,) } }

    var localMediaList = ArrayList<LocalMedia?>()

    //原来状态
    var default_state: Int = -1

    var asstBean: AssetBean? = null

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()
        arguments?.let {
            asstBean = Gson().fromJson(it.getString("bean"), AssetBean::class.java)
        }

//        default_state = asstBean!!.InventoryStatus
        mViewModel.remarksText.set(getString(R.string.remarks) + ":")
        mViewModel.remarks.set(asstBean?.Remarks)

        mViewModel.status.set(getString(R.string.status) + ":")
        var listStatusData = ArrayList<DataBean>()
        listStatusData.add(DataBean(text = getString(R.string.found)))
        listStatusData.add(DataBean(text = getString(R.string.missing)))
        mDatabind.spStatus.adapter = CustomSpinnerAdapter(requireContext(), listStatusData)
        mDatabind.spStatus.setSelection(if (asstBean!!.InventoryStatus == INVENTORY_STOCK) 0 else 1)
        if (asstBean!!.InventoryStatus == INVENTORY_STOCK){
            mDatabind.spStatus.isEnabled = false
        }

        val iamgeList = asstBean!!.imageList
        if (!StringUtils.isEmpty(iamgeList)){
            val split = iamgeList!!.split(UPLOAD_IMAGE_SPLIT)
            split.forEachIndexed(){index, path ->
                if (!StringUtils.isEmpty(path)){
                    localMediaList.add(LocalMedia.generateLocalMedia(activity, path))
                }
            }
        }

        val manager = FullyGridLayoutManager(activity,3, GridLayoutManager.VERTICAL, false)
        mDatabind.recyclerView.setLayoutManager(manager)
        val itemAnimator: RecyclerView.ItemAnimator? = mDatabind.recyclerView.getItemAnimator()
        if (itemAnimator != null) {
            (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        }
        mDatabind.recyclerView.addItemDecoration(
            GridSpacingItemDecoration( 3, DensityUtil.dip2px(activity, 8f), false)
        )
        mDatabind.recyclerView.adapter = imageAdapter
        imageAdapter!!.selectMax = 3
        imageAdapter!!.setOnItemClickListener(object : GridImageAdapter.OnItemClickListener {
            override fun onItemClick(v: View?, position: Int) {
                PictureSelector.create(activity)
                    .openPreview()
                    .setImageEngine(GlideEngine.createGlideEngine())
                    .setExternalPreviewEventListener(object : OnExternalPreviewEventListener {
                        override fun onPreviewDelete(position: Int) {
                            imageAdapter!!.delete(position)
                        }
                        override fun onLongPressDownload(context: Context?, media: LocalMedia?): Boolean {
                            return false
                        }
                    })
                    .startActivityPreview(position, true, imageAdapter!!.data)
            }

            override fun openPicture() {
                PictureSelector.create(activity)
                    .openGallery(SelectMimeType.ofImage())
                    .setMaxSelectNum(3)
                    .setCompressEngine(ImageFileCompressEngine())
                    .setImageEngine(GlideEngine.createGlideEngine())
                    .setSelectedData(imageAdapter!!.data)
                    .forResult(object : OnResultCallbackListener<LocalMedia?> {
                        override fun onResult(result: ArrayList<LocalMedia?>) {
                            localMediaList.clear()
                            localMediaList.addAll(result)
                            imageAdapter!!.getData().clear()
                            imageAdapter!!.getData().addAll(result)
                            imageAdapter!!.notifyDataSetChanged()
                        }
                        override fun onCancel() {}
                    })
            }

        })
    }

    inner class ProxyClick(){

         fun save(){
            val sb = StringBuffer()
            if (imageAdapter!!.data.size != 0){
                imageAdapter!!.data.forEachIndexed(){i, bean ->
                    if (bean!!.compressPath == null){
                        sb.append(bean!!.path).append(UPLOAD_IMAGE_SPLIT)
                    }else{
                        sb.append(bean!!.compressPath).append(UPLOAD_IMAGE_SPLIT)
                    }
                }
            }
            //保存是否在库
           /* if (default_state != asstBean?.InventoryStatus){
                asstBean!!.InventoryStatus = default_state
            }*/
             if (mDatabind.spStatus.selectedItemPosition == 0){
                 asstBean!!.InventoryStatus = INVENTORY_STOCK
             }else if (mDatabind.spStatus.selectedItemPosition == 1){
                 asstBean!!.InventoryStatus = INVENTORY_NOT
             }else{

             }


            //存图片
            if (sb != null){
                asstBean!!.imageList = sb.toString()
            }
            asstBean!!.Remarks = mViewModel.remarks.get()
            //判断时间在不在
            var scanTime = asstBean!!.scanTime
             if (StringUtils.isEmpty(scanTime) || scanTime.equals("null")){
                asstBean!!.scanTime = TimeUtils.getNowString()
            }
            if (asstBean!!.scanStatus == 0){
                asstBean!!.scanStatus = SCAN_STATUS_MANUALLY
            }
             //JSONObject 会赋值 null
            if (StringUtils.isEmpty(asstBean!!.LabelTag) || asstBean!!.LabelTag.equals("null")){
                asstBean!!.LabelTag = ""
            }
            mViewModel.viewModelScope.launch {
                mViewModel.assetDao.update(asstBean!!)
            }

             if (asstBean!!.InventoryStatus == INVENTORY_STOCK){
                 mDatabind.spStatus.isEnabled = false
             }

            assetModel.epcUploadData.value = asstBean!!
             mViewModel.viewModelScope.launch(Dispatchers.IO) {
                 withContext(Dispatchers.Main) {
                     showMessage(message = getString(R.string.release_success))
                 }
             }
        }

        fun status(){if (asstBean!!.InventoryStatus == INVENTORY_STOCK)return
            XPopup.Builder(context)
                .asCenterList(
                    getText(R.string.status), arrayOf(getString(R.string.found), getString(R.string.missing)),
                    null, default_state - 1
                ) { position, text ->
                    default_state = position + 1
//                    mViewModel.status.set((if (default_state == INVENTORY_STOCK) getText(R.string.found) else getText(R.string.missing)).toString())
                }.show()

        }
    }

}